package com.night.icm.controller.business;

import com.night.icm.common.night_enum.StudyStatus;
import com.night.icm.common.utils.PageDataDTO;
import com.night.icm.common.utils.TermType;
import com.night.icm.model.business.StudentCourseDTO;
import com.night.icm.service.business.service.StudentCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

/**
 * StudentCourseController
 * 学生添加修读课程，并初始化成绩
 * 查看修读课程
 *
 * @author Liao ZhiYong
 * @date 2020/2/12
 */
@Api(tags = "选课管理")
@RestController
@RequestMapping("/api/icm-student-course")
public class StudentCourseController {

    private final StudentCourseService studentCourseService;

    @Autowired
    public StudentCourseController(StudentCourseService studentCourseService) {
        this.studentCourseService = studentCourseService;
    }

    @ApiOperation(value = "分页查找选课信息")
    @GetMapping(value = "")
    public PageDataDTO<StudentCourseDTO> findOnePage(
            @ApiParam(value = "页号，从0开始", required = true, defaultValue = "0") @RequestParam("page") @Min(0) int page,
            @ApiParam(value = "每页记录条数", required = true, defaultValue = "30") @RequestParam("size") @Min(1) @Max(100) int size,
            @ApiParam(value = "排序字段，例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", required = false, defaultValue = "id,desc") String sort,
            @ApiParam(value = "所属学生") @RequestParam(value = "studentId", required = false) String studentId,
            @ApiParam(value = "课程名称") @RequestParam(value = "courseName", required = false) String courseName,
            @ApiParam(value = "授课老师") @RequestParam(value = "teacherName", required = false) String teacherName,
            @ApiParam(value = "所属年度") @RequestParam(value = "year", required = false) String year,
            @ApiParam(value = "所属学期") @RequestParam(value = "termType", required = false) TermType termType,
            @ApiParam(value = "修读状态") @RequestParam(value = "studyStatus", required = false) StudyStatus studyStatus
    ) {
        return this.studentCourseService.findOnePage(page, size, sort, studentId, courseName, teacherName, year, termType, studyStatus);
    }

    @ApiOperation(value = "添加选课信息")
    @PostMapping(value = "")
    public StudentCourseDTO create(@ApiParam(value = "选课信息", required = true) @RequestBody @Validated StudentCourseDTO studentCourseDTO) {
        return this.studentCourseService.create(studentCourseDTO);
    }

    @ApiOperation(value = "删除选课信息")
    @DeleteMapping(value = "/{id}")
    public Boolean delete(@ApiParam(value = "选课id", required = true) @PathVariable(name = "id") String id) {
        // 删除选课信息，同时删除成绩信息
        return this.studentCourseService.delete(id);
    }

    @ApiOperation(value = "更新选课信息")
    @PutMapping(value = "/{id}")
    public StudentCourseDTO update(
            @ApiParam(value = "选课id", required = true) @PathVariable(name = "id") String id,
            @ApiParam(value = "选课信息", required = true) @RequestBody @Validated StudentCourseDTO studentCourseDTO
    ) {
        // 只有未结课的选课信息才能修改
        return this.studentCourseService.update(id, studentCourseDTO);
    }

    @ApiOperation(value = "根据id查找选课信息")
    @GetMapping(value = "/{id}")
    public StudentCourseDTO findById(@ApiParam(value = "选课id", required = true) @PathVariable(name = "id") String id) {
        return this.studentCourseService.findById(id);
    }
}